<?php

namespace app\hkyunsound\controller;

use support\Request;

class IndexController
{
    //不需要登录的接口
    protected $noNeedLogin = ['index'];

    public function index(Request $request)
    {
        $username = $request->post('username','');
        //ahuang
        if(!$username){
            return json(['code'=>0,'msg'=>'缺失参数username']);
        }
        $secret = $request->post('secret','');
        //742c07b682cd6de9851f35b80adb61fd
        if(!$secret){
            return json(['code'=>0,'msg'=>'缺失参数username']);
        }
        $devid = $request->post('devid','');
        if(!$secret){
            return json(['code'=>0,'msg'=>'缺失参数devid']);
        }
        $message = $request->post('message','');
        if(!$message){
            return json(['code'=>0,'msg'=>'缺失参数message']);
        }
        $res = $this->getsoundtoken($username,$secret);
        if($res['code']!=200){
            return json(['code'=>0,'msg'=>$res['message']]);
        }
        $token = $res['data']['token'];
        $url = 'https://open.sw-aiot.com/api/message/sendMsg';
        $header['token'] = $token;
        $data = [
            'devid'=>$devid,
            'time'=>time()*1000,
            'reqid'=>md5(time().rand(1000,9999)),
            'message'=>$message,
        ];
        $res = $this->PostJson($url,$data,$header);
        if($res['code']!=200){
            return json(['code'=>0,'msg'=>$res['data']['message']]);
        }else{
            return json(['code'=>1,'msg'=>$res['data']['message']]);
        }

    }
    public function getsoundtoken($username,$secret){
        $url = 'https://open.sw-aiot.com/api/getToken';
        $data['username'] = $username;
        $str = 'secret='.$secret.'&times='.(round(microtime(true)*1000)).'&username='.$username;
        $data['password'] = strtoupper(md5($str));
        $data['times'] = round(microtime(true)*1000);
        $res = $this->PostJson($url,$data);
        return $res;
    }
    //发送json请求函数
    public function PostJson($url, $data = NULL,$headers=[])
    {
        $curl = curl_init();
        $header =array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length:' . strlen(json_encode($data)),
            'Cache-Control: no-cache',
            'Pragma: no-cache'
        );
        if($headers){
            foreach($headers as $k=>$v){
                $header[] = $k.':'.$v;
            }
        }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!$data) {
            return 'data is null';
        }
        if (is_array($data)) {
            $data = json_encode($data);
        }
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_TIMEOUT, 60); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        $errorno = curl_errno($curl);
        if ($errorno) {
            return $errorno;
        }
        curl_close($curl);
        return json_decode($res,true);
    }
}
